//
//  WYMyDynamicController.m
//  Boss
//
//  Created by admin on 2018/7/4.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYMyDynamicController.h"
#import "WYDynamicCell.h"
#import "WYMYDynamic.h"
@interface WYMyDynamicController ()<UITableViewDataSource,UITableViewDelegate,WYHonorCellDelegate>
{
    BOOL _isRefresh;
}
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSUInteger page;
@end

@implementation WYMyDynamicController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
//    self.tableView.backgroundColor = [UIColor purpleColor];
    [self setupUI];
    [self loadData];
    
    
    WEAKSELF
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _isRefresh=NO;
        weakSelf.page++;
        // [weakSelf loadTableData];
        [weakSelf loadData];
    }];
    
    
}
-(void)loadData
{
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    WEAKSELF
    [WYHttpServer getMyDynamicWithCurrentPage:page WithPageSize:@"15" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            if (_isRefresh) {
                [weakSelf.dataArray removeAllObjects];
            }
            
            
            NSArray *array = [WYMYDynamic getDynamicArrayFromJson:response[@"result"][@"detail"]];
            [weakSelf.dataArray addObjectsFromArray:array];
            [weakSelf.tableView reloadData];
            if ([response[@"result"][@"hasNextPage"] integerValue]) {
                [weakSelf.tableView.mj_footer endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        
        
        
        
    }];
    
    
    
    
}
-(void)setupUI
{
    [self.view addSubview:self.tableView];
//    [self.tableView registerClass:[UITableViewCell class] forHeaderFooterViewReuseIdentifier:@"cell"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYDynamicCell" bundle:nil] forCellReuseIdentifier:@"WYDynamicCell"];
    self.tableView.estimatedRowHeight = 300;
}
#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (!self.vcCanScroll) {
        scrollView.contentOffset = CGPointZero;
    }
    if (scrollView.contentOffset.y <= 0) {
        self.vcCanScroll = NO;
        scrollView.contentOffset = CGPointZero;
        //到顶通知父视图改变状态
        [[NSNotificationCenter defaultCenter] postNotificationName:@"leaveTop" object:nil];
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WYDynamicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYDynamicCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYDynamicCell" owner:nil options:nil].firstObject;
    }
    cell.delegate = self;
    WYMYDynamic *dyc = self.dataArray[indexPath.row];
    [cell setSelectionStyle:(UITableViewCellSelectionStyleNone)];
    [cell setDyc:dyc];
    
    //cell.selfVc = self.selfVC;
    return cell;
}
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//     WYMYDynamic *dyc = self.dataArray[indexPath.row];
//    CGSize strSize = [dyc.content boundingRectWithSize:CGSizeMake(kScreenWidth-32, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size;
//     NSArray * urlArray = [dyc.url componentsSeparatedByString:@","];
//    if (urlArray.count) {
//        if (urlArray.count<=3) {
//            
//            return 140+strSize.height+20 + (kScreenWidth-30-32)/3.0+10;
//        }
//        if (urlArray.count<=6) {
//            return 140+strSize.height+20 + (kScreenWidth-30-32)/3.0*2+20;
//        }
//        if (urlArray.count<=9) {
//            return  140+strSize.height+20 + (kScreenWidth-30-32)/3.0*3+30;
//        }
//        
//        
//    }
//    
//    
//    return 140+20+strSize.height;
//    
//}








-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     WYMYDynamic *dyc = self.dataArray[indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"myDyc" object:@{@"service":dyc}];
    
}
-(void)didChangeHeight:(UITableViewCell *)cell
{
    [self.tableView reloadData];
}
-(void)deleteActionWithDymic:(WYMYDynamic *)dyc
{
   WEAKSELF
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您确定要删除这条动态吗" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf deleteActionWithMessageID:dyc.messageId];
        //[alert dismissViewControllerAnimated:YES completion:nil];
    }];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        //[alert dismissViewControllerAnimated:YES completion:nil];
        
    }];
    [alert addAction:action];
    [alert addAction:action1];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}
-(void)deleteActionWithMessageID:(NSString *)messageId
{
    
    WEAKSELF
    [WYHttpServer deleteMyDynamicWithMessageId:messageId SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            _isRefresh = YES;
            [weakSelf loadData];
        }
        
        
        
    }];
    
    
}
-(UITableView *)tableView
{
    if (!_tableView) {
        
        CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-self.navHeight-self.tabHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc]init];
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}


@end
